home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / BoxMaker++ / ll-R ƒ / ll_r_shell.cp next >
Encoding:
Text File  |  1995-01-20  |  6.5 KB  |  298 lines  |  [TEXT/KAHL]

  1. #include <Types.h>
  2. #include <Memory.h>
  3. #include <QuickDraw.h>
  4. #include <OSUtils.h>
  5. #include <ToolUtils.h>
  6. #include <Menus.h>
  7. #include <Packages.h>
  8. #include <Traps.h>
  9. #include <Files.h>
  10. #include <Aliases.h>
  11. #include <AppleEvents.h>
  12. #include <GestaltEqu.h>
  13. #include <Processes.h>
  14. #include <Fonts.h>
  15. #include <OSEvents.h>
  16. #include <Resources.h>
  17. #include <Desk.h>
  18.  
  19. #include "boxmaker constants.h"
  20. #include "boxmaker.h"
  21. #include "preferences.cp"
  22. #include "ll_r_shell.h"
  23. #include "outfile.h"
  24.  
  25. #pragma template_access public
  26.  
  27. void main()
  28. {
  29.     StringHandle prefsFileHandle = GetString( 128);
  30.  
  31.     ll_r_settings defaultSettings =
  32.     {
  33.         false,        // item 0 is not used in dialogs
  34.         true,        // kType
  35.         true,        // kCreator
  36.         true,        // kSize
  37.         false,        // kCreatDate
  38.         true,        // kModDate
  39.         false,        // kBakDate
  40.         false,        // kTimes
  41.  
  42.         true,        // kTabs
  43.         false,        // kSpaces
  44.         false,        // kAskFileName
  45.         true        // kIndentFolders
  46.     };
  47.     ll_r_shell it( *(Str255 *)*prefsFileHandle, defaultSettings);
  48.     it.run();
  49. }
  50.  
  51. ll_r_shell::ll_r_shell( Str255 prefsFilename, ll_r_settings defaultsettings)
  52.     : boxmaker()
  53.     , ll_r_prefs( prefsFilename, defaultsettings)
  54. {
  55.     indentationLevel = 0;
  56.     outFileNo = 0;
  57.     SetupDialog( gMainDialog);
  58. }
  59.  
  60. void ll_r_shell::OpenDoc( Boolean opening)
  61. {
  62.     StartNewLine();
  63.     if( !itsADirectory())
  64.     {
  65.         if( myPrefs[ kType])
  66.         {
  67.             append( (char *)&theCInfoPBRec.hFileInfo.ioFlFndrInfo.fdCreator, 4);
  68.         }
  69.         if( myPrefs[ kCreator])
  70.         {
  71.             append( (char *)&theCInfoPBRec.hFileInfo.ioFlFndrInfo.fdType, 4);
  72.         }
  73.         if( myPrefs[ kSize])
  74.         {
  75.             Str255 sizeString;
  76.             const long totalsize =
  77.                 KiloBytes( theCInfoPBRec.hFileInfo.ioFlLgLen + theCInfoPBRec.hFileInfo.ioFlRLgLen);
  78.             NumToString( totalsize, sizeString);
  79.             if( myPrefs[ kSpaces])
  80.             {
  81.                 const int spacesBefore = 5 - sizeString[ 0];
  82.                 for( int i = 0; i < spacesBefore; i++)
  83.                 {
  84.                     append( ' ');
  85.                 }
  86.             }
  87.             append( sizeString);
  88.             RemoveLastChar();        // the delimiter added by 'append( sizeString)'
  89.             append( 'K');
  90.             append( delimiter);
  91.         }
  92.         if( myPrefs[ kCreatDate])
  93.         {
  94.             appendDate( theCInfoPBRec.hFileInfo.ioFlCrDat);
  95.         }
  96.         if( myPrefs[ kModDate])
  97.         {
  98.             appendDate( theCInfoPBRec.hFileInfo.ioFlMdDat);
  99.         }
  100.         if( myPrefs[ kBakDate])
  101.         {
  102.             appendDate( theCInfoPBRec.hFileInfo.ioFlBkDat);
  103.         }
  104.     }
  105.     //
  106.     // append filename:
  107.     //
  108.     append( theCInfoPBRec.hFileInfo.ioNamePtr);
  109.     ChangeLastToNewLine();
  110.  
  111.     long numtowrite = currentEnd - plentySpace;
  112.  
  113.     (void)FSWrite( outFileNo, &numtowrite, (Ptr)plentySpace);
  114. }
  115.  
  116. void ll_r_shell::CantEnterFolder( Boolean opening)
  117. {
  118.     StartNewLine();
  119.     //
  120.     // 'OpenDoc' wrote a line with the name of the folder on it and ended
  121.     // it with a newline. We want to add an error message to that line.
  122.     // Therefore we overwrite that newline by the first character of the
  123.     // error message (which starts with a tab).
  124.     //
  125.     StringHandle message = GetString( 131);
  126.  
  127.     append( *message);
  128.  
  129.     ChangeLastToNewLine();
  130.  
  131.     long numtowrite = currentEnd - plentySpace;
  132.     (void)SetFPos( outFileNo, fsFromLEOF, -1);
  133.     (void)FSWrite( outFileNo, &numtowrite, (Ptr)plentySpace);
  134. }
  135.  
  136. ll_r_shell::~ll_r_shell()
  137. {
  138.     if( outFileNo != 0)
  139.     {
  140.         (void)FSClose( outFileNo);
  141.         (void)FlushVol( 0, outVRefNum);
  142.     }
  143. }
  144.  
  145. void ll_r_shell::StartABunch( long numTopLevelItems, Boolean opening)
  146. {
  147.     boxmaker::StartABunch( numTopLevelItems, opening);
  148.     if( outFileNo == 0)
  149.     {
  150.         StringHandle outName = GetString( 129);
  151.         if( myPrefs[ kAskFileName])
  152.         {
  153.             StandardFileReply stdReply;
  154.             StringHandle prompt = GetString( 130);
  155.             StandardPutFile( *prompt, *outName, &stdReply);
  156.             ReleaseResource( (Handle)prompt);
  157.             if( stdReply.sfGood)
  158.             {
  159.                 OSErr result = noErr;
  160.                 if( stdReply.sfReplacing)
  161.                 {
  162.                     result = FSpDelete( &stdReply.sfFile);
  163.                 }
  164.                 if( result == noErr)
  165.                 {
  166.                     result = FSpCreate( &stdReply.sfFile,
  167.                                 'R*ch', 'TEXT', stdReply.sfScript);
  168.                     if( result == noErr)
  169.                     {
  170.                         result = FSpOpenDF( &stdReply.sfFile, fsWrPerm, &outFileNo);
  171.                         outVRefNum = stdReply.sfFile.vRefNum;
  172.                     }
  173.                 }
  174.                 if( result != noErr)
  175.                 {
  176.                     ExitToShell();    // should give a nice message before…
  177.                 }
  178.             } else {
  179.                 ExitToShell();    // should give a nice message before…
  180.             }
  181.         } else {
  182.             Handle folderTypeHandle = Get1Resource( 'Wher', 128);
  183.             const OSType folderType = **((OSType **)folderTypeHandle);
  184.             ReleaseResource( folderTypeHandle);
  185.             outfile desktopFile( *(Str255 *)*outName, folderType);
  186.             outFileNo  = desktopFile();
  187.             outVRefNum = desktopFile.vRefNum();
  188.         }
  189.         ReleaseResource( (Handle)outName);
  190.     }
  191. }
  192.  
  193. void ll_r_shell::SetupDialog( DialogPtr theDialog)
  194. {
  195.     short    iType;
  196.     Handle    iHandle;
  197.     Rect    iRect;
  198.  
  199.     for( int i = 1; i < kNumButtons; i++)
  200.     {
  201.         GetDItem( theDialog, i, &iType, &iHandle, &iRect);
  202.         SetCtlValue( (ControlHandle)iHandle, myPrefs[ i]);
  203.     }
  204.     delimiter = "\t "[ myPrefs[ kSpaces]];
  205.  
  206.     const int enable_kTimes = myPrefs[ kCreatDate]
  207.             || myPrefs[ kModDate] || myPrefs[ kBakDate];
  208.  
  209.     GetDItem( theDialog, kTimes, &iType, &iHandle, &iRect);
  210.     HiliteControl( (ControlHandle) iHandle, enable_kTimes ? 0 : 255);
  211. }
  212.  
  213. void ll_r_shell::HandleDialogEvent( short itemHit, DialogPtr theDialog)
  214. {
  215.     switch( itemHit)
  216.     {
  217.         case kType:
  218.         case kCreator:
  219.         case kSize:
  220.         case kTimes:
  221.         case kAskFileName:
  222.         case kIndentFolders:
  223.             FlipButton( theDialog, itemHit);
  224.             break;
  225.  
  226.         case kCreatDate:
  227.         case kModDate:
  228.         case kBakDate:
  229.             {
  230.                 short    iType;
  231.                 Handle    iHandle;
  232.                 Rect    iRect;
  233.     
  234.                 FlipButton( theDialog, itemHit);
  235.     
  236.                 const int enable_kTimes = myPrefs[ kCreatDate]
  237.                         || myPrefs[ kModDate] || myPrefs[ kBakDate];
  238.     
  239.                 GetDItem( theDialog, kTimes, &iType, &iHandle, &iRect);
  240.                 HiliteControl( (ControlHandle) iHandle, enable_kTimes ? 0 : 255);
  241.             }
  242.             break;            
  243.  
  244.         case kTabs:
  245.         case kSpaces:
  246.             if( !myPrefs[ itemHit])
  247.             {
  248.                 FlipButton( theDialog, kTabs);
  249.                 FlipButton( theDialog, kSpaces);
  250.                 
  251.                 delimiter = "\t "[ itemHit == kSpaces];
  252.             }
  253.             break;
  254.     }
  255. }
  256.  
  257. void ll_r_shell::appendDate( unsigned long seconds)
  258. {
  259.     DateTimeRec theDate;
  260.     Secs2Date( seconds, &theDate);
  261.     
  262.     append2Digits( theDate.year);
  263.     append2Digits( theDate.month);
  264.     append2Digits( theDate.day);
  265.     append( delimiter);
  266.  
  267.     if( myPrefs[ kTimes])
  268.     {
  269.         append2Digits( theDate.hour);
  270.         append( ':');
  271.         append2Digits( theDate.minute);
  272.         append( ':');
  273.         append2Digits( theDate.second);
  274.         append( delimiter);
  275.     }
  276. }
  277.  
  278. void ll_r_shell::FlipButton( DialogPtr theDialog, short theItem)
  279. {
  280.     short    iType;
  281.     Handle    iHandle;
  282.     Rect    iRect;
  283.  
  284.     myPrefs[ theItem] = !myPrefs[ theItem];
  285.  
  286.     GetDItem( theDialog, theItem, &iType, &iHandle, &iRect);
  287.     SetCtlValue( (ControlHandle)iHandle, myPrefs[ theItem]);
  288. }
  289.  
  290. void ll_r_shell::append( char *item, int len)
  291. {
  292.     for( int i = 0; i < len; i++)
  293.     {
  294.         *currentEnd++ = *item++;
  295.     }
  296.     append( delimiter);
  297. }
  298.